/*
 * @Author: your name
 * @Date: 2021-03-30 20:14:25
 * @LastEditTime: 2021-05-21 20:28:19
 * @LastEditors: Please set LastEditors
 * @Description: 首页
 * @FilePath: /medicine-web/src/store/modules/main.js
 */
import rest from './../rest'
const main = {
  state: {
    searchValue: '',
    optionsList: {},
    dicts: {},
    is_menus: false, // 开始（true）和关闭（false）menus
    totalNum: 0, // 搜索数量
    resetCurrentTab: false, // 重置主页搜索时，选择全部搜索时下方的menu tab
    refreshTopTab: '未知用户', // 更新Top Bar 用户名
  },
  mutations: {
    SET_SEARCH_CONTENT (state, data) {
      state.searchValue = data
    },
    REFRESH_SENIOR_OPTIONS (state, data) {
      state.optionsList = data
    },
    SET_SYSTEM_DICTS (state, data) {
      state.dicts = data
    },
    SET_MENUS (state, data) {
      state.is_menus = data
    },
    SET_TOTAL_NUM (state, data) {
      state.totalNum = data
    },
    RESET_CURRENT_TAB (state, data) {
      state.resetCurrentTab = !state.resetCurrentTab
    },
    RERESH_TOP_TAB (state, data) {
      state.refreshTopTab = data
    }
  },
  actions: {
    setSearchValue({ commit }, data) { // 搜索 高亮
      commit('SET_SEARCH_CONTENT', data)
    },
    refreshSeniorOptions({ commit }, data) { // 高级选项
      commit('REFRESH_SENIOR_OPTIONS', data)
    },
    setMenus({ commit }, data) { // 开始（true）和关闭（false）menus
      commit('SET_MENUS', data)
    },
    setTotalNum({ commit }, data) { // 设置数量(分页用)
      commit('SET_TOTAL_NUM', data)
    },
    resetCurrentTab({ commit }, data) { // 重置主页搜索时，选择全部搜索时下方的menu tab
      commit('RESET_CURRENT_TAB', data)
    },
    async getInitData({ commit }, data) { // 初始化数据
      const res = await rest.get('pub/get_init_data')
      if (res && res.code == 200) {
        commit('SET_SYSTEM_DICTS', res.data)
        return res.data
      }
      return {}
    },
    async getUpdateData({ commit }, data) { // 获取新闻与更新
      const res = await rest.get('pub/get_update_list')
      if (res && res.code == 200 && res.data && res.data.data_list) {
        return res.data.data_list
      }
      return []
    },
    async getSinglePageData({ commit }, data) { // 获取单页内容
      const res = await rest.get('pub/get_page_info', data)
      if (res && res.code === 200) {
        return res.data
      }
      return {}
    },
    async getLoginSign({ commit }, data) { // 获取sign登录
      const res = await rest.get('auth/auth', data)
      if (res && res.code === 200) {
        return res
      }
      return {}
    },
    refreshTopTab ({ commit }, data) {
      commit('RERESH_TOP_TAB', data)
    }
  },
  getters: {
    searchValue: (state) => state.searchValue, // 搜索数据
    optionsList: (state) => state.optionsList, // 高级选项数据
    dicts: (state) => state.dicts,
    is_menus: (state) => state.is_menus, // 是否关闭menus
    totalNum: (state) => state.totalNum, // 总数
    resetCurrentTab: (state) => state.resetCurrentTab, // 重置主页搜索时，选择全部搜索时下方的menu tab
    refreshTopTab: (state) => state.refreshTopTab // 更新Top Bar 用户名
  }
}
export default main
